package com.atguigu.test.dacahngmainshitidierji.lock.recorsion;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 2 * @Author: madonghui
 * 3 * @Date: 2020/11/10 11:16
 * 4
 */
class Phone implements Runnable{
    public synchronized void sendSMS()throws  Exception{
        System.out.println(Thread.currentThread().getName()+"\t  invoked sendSMS()");
        sendMail();
    }
    public synchronized void sendMail()throws Exception{
        System.out.println(Thread.currentThread().getName()+"\t====invoked sendmail()");
    }

    Lock lock = new ReentrantLock();
    @Override
    public void run() {
        get();
    }

    public void get(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"\t invoked get()");
            set();
        }catch (Exception e){}finally {
            lock.unlock();
        }
    }

    public void set(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"\t"+"&*&&&&&&&&inckoe set()");
        }catch (Exception e){}finally {
            lock.unlock();
        }
    }
}

public class DiguiLock {
    public static void main(String[] args) {
        Phone phone = new Phone();
        new Thread(()->{
            try {
                phone.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"t1").start();
        new Thread(()->{
            try {
                phone.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"t2").start();

        Thread thread = new Thread(phone);
        Thread thread1 = new Thread(phone);
        thread.start();
        thread1.start();
    }
}
